package org.jeecg.modules.supplier.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.supplier.entity.qualification;
import org.jeecg.modules.supplier.service.IQualificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;

/**
 * @Description: 资质信息
 * @Author: jeecg-boot
 * @Date: 2025-09-18
 * @Version: V1.0
 */
@Api(tags = "供应商资质信息66")
@RestController
@RequestMapping("procurement/qualification")
@Slf4j
public class QualificationController extends JeecgController<qualification, IQualificationService> {

    @Autowired
    private IQualificationService qualificationService;

    /**
     * 分页列表查询
     *
     * @return
     */
    @AutoLog(value = "资质信息-分页列表查询")
    @ApiOperation(value = "资质信息-分页列表查询", notes = "资质信息-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        // 保持原有逻辑，分页参数有默认值，无需额外校验
        Page<qualification> page = new Page<qualification>(pageNo, pageSize);
        IPage<qualification> pageList = qualificationService.page(page);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param qualification
     * @return
     */
    @AutoLog(value = "资质信息-添加")
    @ApiOperation(value = "资质信息-添加", notes = "资质信息-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody qualification qualification) {
        // 确保qualification对象不为空
        if (qualification == null) {
            log.warn("【资质信息】添加失败: 资质对象为空");
            return Result.error("资质对象不能为空");
        }
        qualification.setCreateTime(new Date());
        qualificationService.save(qualification);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param qualification
     * @return
     */
    @AutoLog(value = "资质信息-编辑")
    @ApiOperation(value = "资质信息-编辑", notes = "资质信息-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody qualification qualification) {
        // 确保qualification对象不为空
        if (qualification == null) {
            log.warn("【资质信息】编辑失败: 资质对象为空");
            return Result.error("资质对象不能为空");
        }
        qualification.setUpdateTime(new Date());
        qualificationService.updateById(qualification);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "资质信息-通过id删除")
    @ApiOperation(value = "资质信息-通过id删除", notes = "资质信息-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        // 安全校验：即使required=true，也防止空字符串
        if (StringUtils.isEmpty(id)) {
            log.warn("【资质信息】删除失败: id不能为空");
            return Result.error("id不能为空");
        }
        qualificationService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "资质信息-批量删除")
    @ApiOperation(value = "资质信息-批量删除", notes = "资质信息-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        // 优化错误提示信息，使用更明确的提示
        if (StringUtils.isEmpty(ids)) {
            log.warn("【资质信息】批量删除失败: ids不能为空");
            return Result.error("ids不能为空");
        }
        this.qualificationService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "资质信息-通过id查询")
    @ApiOperation(value = "资质信息-通过id查询", notes = "资质信息-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        // 安全校验：即使required=true，也防止空字符串
        if (StringUtils.isEmpty(id)) {
            log.warn("【资质信息】查询失败: id不能为空");
            return Result.error("id不能为空");
        }
        qualification qualification = qualificationService.getById(id);
        if (qualification == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(qualification);
    }

    /**
     * 根据供应商ID查询资质列表
     *
     * @param supplierId
     * @return
     */
    @AutoLog(value = "资质信息-根据供应商ID查询资质列表")
    @ApiOperation(value = "资质信息-根据供应商ID查询资质列表", notes = "资质信息-根据供应商ID查询资质列表")
    @GetMapping(value = "/getBySupplierId")
    public Result<?> getBySupplierId(@RequestParam(name = "supplierId", required = true) String supplierId) {
        // 安全校验：即使required=true，也防止空字符串
        if (StringUtils.isEmpty(supplierId)) {
            log.warn("【资质信息】查询失败: supplierId不能为空");
            return Result.error("supplierId不能为空");
        }
        QueryWrapper<qualification> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("supplier_id", supplierId);
        queryWrapper.orderByDesc("create_time");
        return Result.OK(qualificationService.list(queryWrapper));
    }
}
